package com.fzc.rhinooffice.module;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.fzc.rhinooffice.module.entity.Sms;

import java.util.List;

public abstract class CommonAdapter<T> extends BaseAdapter {
    protected Context context;
    protected List<T> datas;
    protected LayoutInflater mInflater;
    protected int leftLayoutId;

    protected int rightLayoutId;

    protected String match;

    //是不是聊天模式
    protected boolean ischat=false;

    public CommonAdapter(Context context, List<T> datas, int layoutId) {
        this.datas = datas;
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.leftLayoutId = layoutId;
    }

    public CommonAdapter(Context context, List<T> datas, int leftLayoutId, int rightLayoutId,String match) {
        this.datas = datas;
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.leftLayoutId = leftLayoutId;
        this.rightLayoutId = rightLayoutId;
        this.ischat=true;
        this.match = match;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public T getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (ischat){
            Sms sms = (Sms) datas.get(position);
            if (sms.from_name.equals(match)){
                holder = getViewHolder(position, convertView, parent,false);
            }else {
                holder = getViewHolder(position, convertView, parent,true);
            }

        }else {
            holder = getViewHolder(position, convertView, parent);
        }
        convert(holder, getItem(position));
        return holder.getConvertView();
    }

    //通过viewholder获得组件，通过item获得组件所要呈现的信息
    public abstract void convert(ViewHolder holder, T item);

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {

        return ViewHolder.get(context, convertView, parent, leftLayoutId, position);
    }

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent, boolean isleft) {

        if (isleft) {
            return ViewHolder.get(context, convertView, parent, leftLayoutId, position);
        } else {
            return ViewHolder.get(context, convertView, parent, rightLayoutId, position);
        }

    }

}
